package cn.bugstack.initializr.rigger.domain.service;

import cn.bugstack.initializr.rigger.application.IProjectGenerator;
import cn.bugstack.initializr.rigger.domain.model.ProjectInfo;
import cn.bugstack.initializr.rigger.domain.service.module.impl.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URL;
import java.util.Arrays;

/**
 * 从 https://bugstack.cn  改造而来
 * 
 * Create by 小傅哥(fustack)
*  update by 小太阳
 */
@Service
public class ProjectGeneratorImpl implements IProjectGenerator {

    private Logger logger = LoggerFactory.getLogger(ProjectGeneratorImpl.class);

    @Resource
    private GenerationApplication generationApplication;
    @Resource
    private GenerationYml generationYml;
    @Resource
    private GenerationPom generationPom;
    @Resource
    private GenerationTest generationTest;
    @Resource
    private GenerationIgnore generationIgnore;
    @Resource
    private GenerationPackageInfo generationPackageInfo;

    @Resource
    private GenerationLogBackInfo  generationLogBackInfo;

    @Resource
    private GenerationShellInfo generationShellInfo;

    @Resource
    private GenerationGeneratorInfo generationGeneratorInfo;

    @Resource
    private GenerationDockerFile  generationDockerFile;

    @Resource
    private GenerationK8s generationK8s;

    @Resource
    private GenerationEhCacheConfig generationEhCacheConfig;

    @Resource
    private GenerationDockerCompose generationDockerCompose;

    @Resource
    private GenerationWebLogAspectAopConfigInfo generationWebLogAspectAopConfigInfo;

    @Resource
    private GenerationMethodLogInfo generationMethodLogInfo;


    @Override
    public void generator(ProjectInfo projectInfo) throws Exception {

        URL resource = this.getClass().getResource("/");
        String projectsRoot = resource.getFile() + "/projects/";

        String lastPackageName = "crud";//projectInfo.getArtifactId().replaceAll("-", "").toLowerCase();
        //启动类名称
        String[] split = projectInfo.getArtifactId().split("-");
        StringBuffer applicationJavaName = new StringBuffer();
        Arrays.asList(split).forEach(s -> {
            applicationJavaName.append(s.substring(0, 1).toUpperCase() + s.substring(1));
        });
        applicationJavaName.append("Application");

        // 1. 创建  Application.java
        generationApplication.doGeneration(projectInfo, projectsRoot, lastPackageName, applicationJavaName);

        // 2. 生成 application.yml
        generationYml.doGeneration(projectInfo, projectsRoot);

        // 3. 生成 pom.xml
        generationPom.doGeneration(projectInfo, projectsRoot);

        // 4. 创建测试类 ApiTest.java
        generationTest.doGeneration(projectInfo, projectsRoot, lastPackageName, applicationJavaName);

        // 5. 生成 .gitignore
        generationIgnore.doGeneration(projectInfo, projectsRoot);

        // 6. DDD 四层描述文件
       // generationPackageInfo.doGeneration(projectInfo, projectsRoot, lastPackageName, applicationJavaName);

        // 7. 生成 logback-spring.xml
        generationLogBackInfo.doGeneration(projectInfo,projectsRoot);

        //8. 生成启动的shell 脚本
        generationShellInfo.doGeneration(projectInfo,projectsRoot,lastPackageName);

        //9 生成代码生成器
        generationGeneratorInfo.doGeneration(projectInfo,projectsRoot);

        // 10  生成  dockerFile
        generationDockerFile.doGeneration(projectInfo,projectsRoot);

        //11 生成  k8s.yaml
        generationK8s.doGeneration(projectInfo,projectsRoot);

        String ehCacheClassName="EhCacheConfig";
        //12 创建  EhCacheConfigInfo.java
        generationEhCacheConfig.doGeneration(projectInfo, projectsRoot, lastPackageName,ehCacheClassName);

        //13 创建 docker-compose.yml
        generationDockerCompose.doGeneration(projectInfo,projectsRoot);

        //14创建WebLogAspectAopConfig.java
        generationWebLogAspectAopConfigInfo.doGeneration(projectInfo, projectsRoot, lastPackageName,"WebLogAspectAopConfig");

        //15 创建MethodLog.java
        generationMethodLogInfo.doGeneration(projectInfo, projectsRoot, lastPackageName,"MethodLog");

    }

}
